import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Connection conn = null;
        PreparedStatement pstm = null;
        int id= Integer.parseInt(request.getParameter("id"));
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true";
            String user = "root";
            String pwd = "123456";
            conn = DriverManager.getConnection(url, user, pwd);
            String sql = "delete from music where id=?";
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, id);
            int set = pstm.executeUpdate();
            System.out.println(set);
            if (pstm != null) {
                pstm.close();
            }
            if (conn != null) {
                conn.close();
            }
            response.sendRedirect("SqlServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
